package com.example.schoolexam2.controller;

import com.example.schoolexam2.dto.StudentDto;
import com.example.schoolexam2.entity.Student;
import com.example.schoolexam2.enums.ResultEnum;
import com.example.schoolexam2.service.StudentService;
import com.example.schoolexam2.utils.fileFormatUtils;
import com.example.schoolexam2.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService StudentService;


    @PostMapping("/studetnPhoto")
    ResultVO<Integer> studetnPhoto(@RequestPart MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return new ResultVO<>(500,"文件为空",null);
        }
        Map<String, String> fileMap = fileFormatUtils.FileName(file);

        return null;
    }

    @PostMapping("/studentIncreases")
    ResultVO<Integer> studentIncreases(@RequestPart MultipartFile file){
        if (file.isEmpty()) {
            System.out.println("文件为空");
            //没有上传文件
            return new ResultVO(ResultEnum.IMPORT_FAILED.getCode(), ResultEnum.IMPORT_FAILED.getMessage(), null);
        }
        int RequestType = fileFormatUtils.excelType(file);
        if (RequestType == 2) {
            return new ResultVO(500, "格式不正确", null);
        }
        try {
            int token = StudentService.studentIncreases(file, RequestType);
            if (token != 0) {
                return new ResultVO<>(ResultEnum.IMPORT_SUCCESS.getCode(), ResultEnum.IMPORT_SUCCESS.getMessage(), token);
            } else {
                return new ResultVO<>(ResultEnum.IMPORT_FAILED.getCode(), ResultEnum.IMPORT_FAILED.getMessage(), null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/studentIncrease")
    ResultVO<Integer> studentIncrease( StudentDto student){
        ResultVO<Integer> resultVO;
        int token = StudentService.studentIncrease(student);
        if (token!= 0 ){
            resultVO=new ResultVO<>(ResultEnum.IMPORT_SUCCESS.getCode(), ResultEnum.IMPORT_SUCCESS.getMessage(), token);
        }else{
            resultVO=new ResultVO<>(ResultEnum.IMPORT_FAILED.getCode(), ResultEnum.IMPORT_FAILED.getMessage(), null);
        }
        return resultVO;
    }

    @PostMapping("/studentDelete")
    ResultVO<Integer> studentDelete(String studentId,int isUser){
        ResultVO<Integer> resultVO;
        int token = StudentService.studentDelete(studentId,isUser);
        if (token!= 0 ){
            resultVO=new ResultVO<>(ResultEnum.MODIFY_SUCCESS.getCode(), ResultEnum.MODIFY_SUCCESS.getMessage(), token);
        }else{
            resultVO=new ResultVO<>(ResultEnum.MODIFY_FAILURE.getCode(), ResultEnum.MODIFY_FAILURE.getMessage(), null);
        }
        return resultVO;
    }

    @PostMapping("/studentUpdate")
    ResultVO<Integer> studentUpdate( Student student){
        ResultVO<Integer> resultVO;
        int token = StudentService.studentUpdate(student);
        if (token!= 0 ){
            resultVO=new ResultVO<>(ResultEnum.MODIFY_SUCCESS.getCode(), ResultEnum.MODIFY_SUCCESS.getMessage(), token);
        }else{
            resultVO=new ResultVO<>(ResultEnum.MODIFY_FAILURE.getCode(), ResultEnum.MODIFY_FAILURE.getMessage(), null);
        }
        return resultVO;
    }

    @PostMapping("/studentPersonal")
    ResultVO<List<Student>> studentPersonal( String studentId){
        ResultVO<List<Student>> resultVO;
        List<Student> token = StudentService.studentPersonal(studentId);
        if (token!= null ){
            resultVO=new ResultVO<>(ResultEnum.MODIFY_SUCCESS.getCode(), ResultEnum.MODIFY_SUCCESS.getMessage(), token);
        }else{
            resultVO=new ResultVO<>(ResultEnum.MODIFY_FAILURE.getCode(), ResultEnum.MODIFY_FAILURE.getMessage(), null);
        }
        return resultVO;
    }
    //模糊查询
    @PostMapping("/studentFuzzy")
    ResultVO<List<Student>> studentFuzzy( String fuzzy){
        ResultVO<List<Student>> resultVO;
        List<Student> token = StudentService.studentFuzzy(fuzzy);
        if (token!= null ){
            resultVO=new ResultVO<>(ResultEnum.MODIFY_SUCCESS.getCode(), ResultEnum.MODIFY_SUCCESS.getMessage(), token);
        }else{
            resultVO=new ResultVO<>(ResultEnum.MODIFY_FAILURE.getCode(), ResultEnum.MODIFY_FAILURE.getMessage(), null);
        }
        return resultVO;
    }
    //班级学生
    @PostMapping("/studentAll")
    ResultVO<List<Student>> studentAll(@RequestParam("classId")  String classId){
        System.out.println(classId);
        ResultVO<List<Student>> resultVO;
        List<Student> token = StudentService.studentAll(classId);
        if (token!= null ){
            resultVO=new ResultVO<>(200, "获取班级信息成功", token);
        }else{
            resultVO=new ResultVO<>(ResultEnum.MODIFY_FAILURE.getCode(), ResultEnum.MODIFY_FAILURE.getMessage(), null);
        }
        return resultVO;
    }

    @GetMapping("/get")
    String get(){
        return "请求成功";
    }

}
